package com.haier.bims.fridge.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.haier.bims.fridge.bean.FreezeBox;
import com.haier.bims.fridge.bean.FreezeDevice;
import com.haier.bims.fridge.bean.FreezeDrawerModel;
import com.haier.bims.fridge.bean.FreezeLevel;
import com.haier.bims.fridge.bean.FreezeShelves;
import com.haier.bims.fridge.bean.Fridge;
import com.haier.bims.fridge.bean.Room;
import com.haier.bims.fridge.dao.FridgeDAO;
import com.haier.bims.fridge.service.IFridgeService;
import com.haier.bims.frozenbox.bean.Frozenbox;
import com.haier.bims.frozenbox.dao.FrozenboxDAO;

@Component
public class FridgeServiceImpl implements IFridgeService {
	
	private FridgeDAO fridgeDAO;
	
	private FrozenboxDAO frozenboxDAO;

	@Autowired
	public void setFridgeDAO(FridgeDAO fridgeDAO) {
		this.fridgeDAO = fridgeDAO;
	}

	@Autowired
	public void setFrozenboxDAO(FrozenboxDAO frozenboxDAO) {
		this.frozenboxDAO = frozenboxDAO;
	}

	@Override
	public List<Room> getRoomInfo() {
		// TODO Auto-generated method stub
		List<Fridge> list = this.fridgeDAO.getRoomInfo();
		List<Room> listroom = new ArrayList<Room>();
		List<String> ids=new ArrayList<String>();
		for(int i=0;i<list.size();i++){
			Fridge fridge = list.get(i);
			String roomid = fridge.getRoomId();
			boolean ishave = false;
			if(i>0){
				ishave = ids.contains(roomid);
			}
			if(ishave==false){
				ids.add(roomid);
			}else{
				continue;
			}
			
		}
		for(int j=0;j<ids.size();j++){
			Room room = getRoom(ids.get(j), list);
			listroom.add(room);
		}
		return listroom;
	}

	public Room getRoom(String roomid,List<Fridge> list){
		Room room  = new Room();
		List<Fridge> child = new ArrayList<Fridge>();
		for(int i=0;i<list.size();i++){
			Fridge fridge = list.get(i);
			String rid = fridge.getRoomId();
			if(rid.equals(roomid)){
				room.setId(roomid);
				room.setType(fridge.getRoomtype());
				room.setName(fridge.getRoomName());
				child.add(fridge);
			}else{
				continue;
			}
		}
		room.setChild(child);
		return room;
	}

	@Override
	public List<FreezeDevice> qryDisplayDevice(String id) {
		// TODO Auto-generated method stub
		List<FreezeDevice> devices=null;
	    if(!"".equals(id)){
	    	devices=new ArrayList<FreezeDevice>();
			    //查询得到当前冰箱
			    FreezeDevice device=this.fridgeDAO.qyDeviceById(id);
			    //查询冰箱中的层
			    List<FreezeLevel> levels=this.fridgeDAO.qyLevlBydevId(device);
			    //循环层,查询每个层中的架子
			    Iterator<FreezeLevel> it=levels.iterator();
			    while(it.hasNext()){
				FreezeLevel fl=it.next();//取到当前层
				fl.setId(device.getId());
				//取到当前层下的所有架子
				List<FreezeShelves> shelves=this.fridgeDAO.qyShelvesByLevelId(fl);
				//循环架子,取架子里面的抽屉
				Iterator<FreezeShelves> its=shelves.iterator();
				while(its.hasNext()){
				    FreezeShelves fs=its.next();//取到当前架子
				    fs.setId(fl.getId());
				    //拿到当前架子下的所有抽屉
				    List<FreezeDrawerModel> drawers=this.fridgeDAO.qyDrawerByShelveId(fs);
				    //循环抽屉
				    Iterator<FreezeDrawerModel> itf=drawers.iterator();
				    while(itf.hasNext()){
					//取得当前抽屉
					FreezeDrawerModel fd=itf.next();
					FreezeDrawerModel fd_count=this.fridgeDAO.qyDrawerCount(fd);
					fd.setId(fs.getId());
					fd.setCount(fd_count.getCount());
					//查询抽屉下的冻存盒
					List<FreezeBox> boxs=this.fridgeDAO.qyBoxByDrawerId(fd);
					Iterator<FreezeBox> itb=boxs.iterator();
					while(itb.hasNext()){
					    FreezeBox fb=itb.next();
					    fb.setId(fd.getId());
					}
					//把冻存盒当到抽屉里
					fd.setChild(boxs);
				    }
				    //把抽屉放到架子里
				    fs.setChild(drawers);
				}
				//把架子放到层里
				fl.setChild(shelves);
			    }
			    //把层放到冰箱里
			    device.setChild(levels);
			    devices.add(device);
	    }
	    return devices;
	}

	@Override
	public List<Frozenbox> qryDrawerBoxes(String id) {
		// TODO Auto-generated method stub
		List<Frozenbox> boxes = new ArrayList<Frozenbox>();
		//先获得所有盒子ids
		List<String> list = new ArrayList<String>();
		FreezeDrawerModel fd = new FreezeDrawerModel();
		fd.setObjid(id);
		List<FreezeBox> listids = this.fridgeDAO.qyBoxByDrawerId(fd);
		for(int i=0;i<listids.size();i++){
			list.add(listids.get(i).getObjid());
		}
		//获得所有盒子列表
		boxes = this.frozenboxDAO.getFrozenboxInfo(list);
		return boxes;
	}
	
}
